// index.js
// 获取应用实例
const app = getApp()
const {
    getBanners,
    getVolunteerTotal,
    getCommunityTotal,
    getActivityTotal,
    getHotActivity
} = require('../../services/indexReq')

const {
    getActivityContent
} = require('../../services/activityContentReq')

Page({
    data: {
        banners: [],
        hotList: [],
        contentlist: [],
        communityCount: 0,
        volunteerCount: 0,
        activityCount: 0,
        pageNum: 1,
        pageSize: 10,
        isFinish: false
    },

    onShow() {
        // 获取海报
        getBanners(res => {
            this.setData({
                banners: res.data.data
            })
        })

        // 获取志愿者总数
        getVolunteerTotal(res => {
            this.setData({
                volunteerCount: res.data.data
            })
        })

        // 获取社团总数
        getCommunityTotal(res => {
            this.setData({
                communityCount: res.data.data
            })
        })

        // 获取志愿活动总数
        getActivityTotal(res => {
            this.setData({
                activityCount: res.data.data
            })
        })

        // 获取全部活动风采
        this.handleGetAllActivity()
    },

    onReachBottom() {
        // 获取全部活动风采
        this.handleGetAllActivity()
    },

    // 获取全部活动风采
    handleGetAllActivity() {
        if (this.data.isFinish) {
            return;
        }
        wx.showLoading({
            title: '加载中',
            mask: true,
        })

        getActivityContent(this.data.pageNum, this.data.pageSize, res => {
            console.log(res.data)
            let list = res.data.data.list
            if (list.length < this.data.pageSize) {
                this.setData({
                    isFinish: true
                })
            }

            this.setData({
                contentlist: this.data.contentlist.concat(list),
                pageNum: this.data.pageNum + 1
            })
            wx.hideLoading()
        })
    },

    // 跳转风采详情
    handleGoDetail(e) {
        const id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../contentDetail/contentDetail?id=' + id,
        })
    },
    // 个人注册
    handleRegisterPersonal() {
        wx.navigateTo({
            url: '../registerPersonal/registerPersonal',
        })
    },

    // 家庭注册
    handleRegisterTeam() {
        if (wx.getStorageSync('token')) {
            this.setData({
                isLogin: true
            })
        } else {
            wx.reLaunch({
                url: '../login/login',
            })
        }
        wx.navigateTo({
            url: '../registerTeam/registerTeam',
        })
    },

    // 加入活动
    handleGoActivity() {
        wx.switchTab({
            url: '../activity/activity',
        })
    },

    // 加入社团
    handleGoCommunityList() {
        wx.navigateTo({
            url: '../communityList/communityList',
        })
    },

    // 跳转课程中心
    handleGoCourse() {
        wx.navigateTo({
            url: '../course/course',
        })
    },
    // 跳转信息查询
    handleGoQuery() {
        wx.showToast({
            title: '暂未开通',
            icon: 'none'
        })
    },

    // 跳转活动风采
    handleGoActivityContent() {
        wx.navigateTo({
            url: '../activityContent/activityContent',
        })
    },
    // 跳转发布投票
    handleGoVote() {
        wx.navigateTo({
            url: '../vote/commit',
        })
    },
    handleGoRegisterFamily() {
        wx.navigateTo({
            url: '../registerTeam/registerTeam',
        })
    },
    // 跳转礼品中心
    handleGoGiftsCenter() {
        wx.navigateTo({
            url: "../gifts/gifts",
        })
    },
    // 跳转常见问题
    handleGoQuestion() {
        wx.showToast({
            title: '暂未开通',
            icon: 'none'
        })
    },

})